package com.minierp.dao.hibernate;

import com.minierp.dao.AccessoryDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class AccessoryDaoImpl extends BaseDaoImpl implements AccessoryDao {
	public List getList(String agentType, long agentId, String[] accType,
			long extId) {
		String sql = "from com.minierp.model.Accessory s where s.agentId=:agentId and s.agentType=:agentType";
		if (accType != null && accType.length != 0) {
			sql = sql + " and (";

			for (int sess = 0; sess < accType.length; ++sess) {
				if (sess > 0) {
					sql = sql + " or ";
				}

				sql = sql + "s.accType=:accType" + sess;
			}

			sql = sql + ")";
		}

		if (extId != 0L) {
			sql = sql + " and s.extId=" + extId;
		}

		sql = sql + " order by s.id";
		Session arg14 = null;

		List arg11;
		try {
			arg14 = this.getSession();
			Query q = arg14.createQuery(sql);
			q.setLong("agentId", agentId);
			q.setString("agentType", agentType);
			if (accType != null && accType.length != 0) {
				for (int i = 0; i < accType.length; ++i) {
					q.setString("accType" + i, accType[i]);
				}
			}

			arg11 = q.list();
		} finally {
			if (arg14 != null) {
				this.releaseSession(arg14);
			}

		}

		return arg11;
	}

	public List getList(String agentType, String tmpCode, String[] accType,
			long extId) {
		String sql = "from com.minierp.model.Accessory s where s.tmpCode=:tmpCode and s.agentType=:agentType";
		if (accType != null && accType.length != 0) {
			sql = sql + " and (";

			for (int sess = 0; sess < accType.length; ++sess) {
				if (sess > 0) {
					sql = sql + " or ";
				}

				sql = sql + "s.accType=:accType" + sess;
			}

			sql = sql + ")";
		}

		if (extId != 0L) {
			sql = sql + " and s.extId=" + extId;
		}

		sql = sql + " order by s.id";
		Session arg13 = null;

		List arg10;
		try {
			arg13 = this.getSession();
			Query q = arg13.createQuery(sql);
			q.setString("tmpCode", tmpCode);
			q.setString("agentType", agentType);
			if (accType != null && accType.length != 0) {
				for (int i = 0; i < accType.length; ++i) {
					q.setString("accType" + i, accType[i]);
				}
			}

			arg10 = q.list();
		} finally {
			if (arg13 != null) {
				this.releaseSession(arg13);
			}

		}

		return arg10;
	}

	public List getList(String agentType, long agentId) {
		String sql = "from com.minierp.model.Accessory s where s.agentId=:agentId and s.agentType=:agentType order by s.id desc";
		Session sess = null;

		List arg7;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setLong("agentId", agentId);
			q.setString("agentType", agentType);
			arg7 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg7;
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.Accessory where id=:id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int delete(final String agentType, final long agentId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.Accessory where agentId=:agentId and agentType=:agentType";
						return new Integer(session.createQuery(hql)
								.setLong("agentId", agentId)
								.setString("agentType", agentType)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int update(final String tmpCode, final long agentId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.Accessory set agentId=:agentId where tmpCode=:tmpCode";
						return new Integer(session.createQuery(hql)
								.setLong("agentId", agentId)
								.setString("tmpCode", tmpCode).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int estCorporate(final long fromEstId, final long toEstId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.Accessory set agentId="
								+ toEstId
								+ " where agentId="
								+ fromEstId
								+ " and agentType=\'ESTABLISHMENT\'";
						return new Integer(session.createQuery(hql)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int changeExtId(String[] ids, final long extId) throws Exception {
		if (ids != null && ids.length != 0) {
			final String tmpStr = "";

			for (int idStr = 0; idStr < ids.length; ++idStr) {
				if (!tmpStr.equals("")) {
					tmpStr = tmpStr + ",";
				}

				tmpStr = tmpStr + Long.parseLong(ids[idStr]);
			}

			Object obj = this.getHibernateTemplate().execute(
					new HibernateCallback() {
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							String hql = "update com.minierp.model.Accessory set extId="
									+ extId + " where id in (" + tmpStr + ")";
							Query q = session.createQuery(hql);
							return new Integer(q.executeUpdate());
						}
					});
			return ((Integer) obj).intValue();
		} else {
			return 0;
		}
	}

	public List getDownloadLogList(String agentType, long agentId) {
		String sql = "select l.createTime,l.createrName,s.sourceName from com.minierp.model.Accessory s,com.minierp.model.DownloadLog l where s.id=l.acceId and s.agentId=:agentId and s.agentType=:agentType order by l.id";
		Session sess = null;

		List arg7;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			q.setLong("agentId", agentId);
			q.setString("agentType", agentType);
			arg7 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg7;
	}
}